// JavaScript Document

function GamingStatus(lgc)
{
	var _this = this;
	var dangoX = undefined;
	var dangoY = undefined;
	
	//得分, 具体算法为背景图上升的距离之和(暂定为这么计算)
	//由于取值偏大, 最后得分取其1/10
	
	LogicStatus.call(_this, lgc);
	// 游戏过程中的bg
	_this.bg = undefined;
	_this.dango = undefined;
	_this.brickManager = undefined;
	_this.scoreBoard = undefined;
	_this.bricks = undefined;
	_this.keydown = undefined;
	_this.firstkeydown = undefined;
	
	// 死的时候就开始计时
	_this.deadTime = 0;
	//death flag(flase为未死亡, true为死亡)
	_this.deathFlag = undefined;
	_this.prepare = function(preStat){
		if(preStat == MENU || preStat == OVER)
		{
			marks = 0;
			init();
            //添加砖块
            _this.bricks = _this.brickManager.createBricks(30);
            for(var i = 0; i < _this.bricks.length; i++)
            {
                var node = _this.logic.sceneManager.createNode( _this.bricks[i], 4);           
                _this.logic.sceneManager.addNode(node);        
            }     
    	}	
		
		if(preStat == PAUSE)
		{
			for(var i = 0; i < _this.bricks.length; i++)
            {
                var node = _this.logic.sceneManager.createNode( _this.bricks[i], 4);           
                _this.logic.sceneManager.addNode(node);        
            }     
		}
		
		
		
	    //添加背景图
        var sNode = _this.logic.sceneManager.createNode( _this.bg, 5)       
        _this.logic.sceneManager.addNode(sNode); 
        //添加角色
        sNode = _this.logic.sceneManager.createNode(_this.dango, 2);
        _this.logic.sceneManager.addNode(sNode);         
		// 添加分数板
		sNode = _this.logic.sceneManager.createNode( _this.scoreBoard, 2);
		_this.scoreBoard.update(marks);            
		_this.logic.sceneManager.addNode(sNode); 
	}

	_this.update = function(){
      	// 在这里实现物件的更新, 
		//背景图的移动
		_this.bg.position.y -= 0.3;
		
		//更新砖块
		_this.brickManager.update(marks);
		
		//判断dango与砖块碰撞, 并使砖块运动反向
		_this.logic.physics.updateObject(_this.dango.physicsNode, 0.5);
		if(_this.deathFlag == false)
		{
			for(var i = 0; i < _this.bricks.length; i++)
			{    
				if(_this.dango.physicsNode.velocity.y > 0)
				{
					if(_this.logic.physics.detectCollision(_this.dango, _this.bricks[i].physicsNode))
					{
						_this.brickManager.touch(_this.bricks[i]);
						if(_this.bricks[i].name == "staticBrick" ||  _this.bricks[i].name == "movingBrick" )
						{
							
							 _this.dango.physicsNode.velocity.y = -22;
						}
					}
				}
			}
			
			//判断dango是否在屏幕中间位置以上
			if(_this.dango.position.y < 300)
			{
				var distanceRest;
				distanceRest = 300 - _this.dango.position.y;
				_this.bg.position.y += distanceRest;
				
				
				_this.brickManager.changeBricksCoordY(distanceRest);
				_this.dango.position.y = 300;
				marks += distanceRest/10;
				// 更新分数板
				_this.scoreBoard.update(marks);
			}
			
			
			//判断dango是否死亡
			if(_this.dango.physicsNode.position.y > 600)
			{
					_this.deathFlag = true;
					// 存得当前时间
					_this.deadTime = _this.logic.time;

			}
			
		}// --------------deathFlag == false===================
		
		// -----------------deathFlag == true==================
		else{
			
			// 让dango飞一会儿
			var dist = 30;
			var duration = 1200;
			var factor = (_this.logic.time - _this.deadTime)/duration;
			dist = dist - dist * factor * factor;
			_this.dango.physicsNode.position.y -= dist;
			_this.bg.position.y -= dist;
			_this.brickManager.changeBricksCoordY(-dist);
			
			
			if(_this.logic.time - _this.deadTime > duration)
			{
				_this.clearUp();
				_this.logic.statusChange(OVER);
			}
		}
		
		//平滑控制dango的移动
		if( _this.dango.physicsNode.velocity.x < 0  &&   _this.keydown == false){
		    _this.dango.physicsNode.velocity.x += 10;
		}else if( _this.dango.physicsNode.velocity.x > 0 &&  _this.keydown == false){
		    _this.dango.physicsNode.velocity.x -= 10;
		}
		
		//判断dango是否超出屏幕左右
		if(_this.dango.physicsNode.position.x > 480)
		{
			_this.dango.physicsNode.position.x = -33;
		}
		if(_this.dango.physicsNode.position.x < -33)
		{
			_this.dango.physicsNode.position.x = 480;
		}
		
		
		
		
		//更新dango PhysicsNode信息与绘画信息
		_this.dango.update();
	    }
	
	_this.mouseClick = function(e){
	 
		 var CanvasEle = document.getElementById("myCanvas");
	    //alert(distanceCanvas.offsetLeft);
	    var mouseX = e.clientX - CanvasEle.offsetLeft;
	    var mouseY = e.clientY - CanvasEle.offsetTop;
			
	}
	
	  _this.mouseMove = function(e)
     {
        //鼠标移动事件
     }
     
    _this.keyUp = function(e)
    {
       var key = e.keyCode;
       _this.keydown = false;
       _this.firstkeydown = true;
    } 
	
	_this.keyDown = function(e){
		// 比如我按键之后转换为状态2 : 就这么做:
		if (_this.firstkeydown == true)
		{
		    _this.firstkeydown = false;
		     _this.dango.physicsNode.velocity.x = 0;
		}
		_this.keydown = true;
		var key = e.keyCode;
		//按下左方向键
		if (key == '37')
		{
		  _this.dango.physicsNode.velocity.x = -10;
		   //_this.dango.physicsNode.position.x -= 5;
        }
		//按下右方向键
	    if (key == '39')
        {
          _this.dango.physicsNode.velocity.x = 10;
          //_this.dango.physicsNode.position.x += 5;
        }
		//按下空格键, 暂停游戏
		if (key == '32')
		{
		  _this.clearUp();
	      _this.logic.statusChange(PAUSE);
		}
	}
	
	// 这个函数用来清理资源
	_this.clearUp = function(){
	    _this.logic.sceneManager.clearNodeList(); 
	}
	// 这个函数用来查询ready状态.
	_this.ready = function(){
		return _this.bg.ready && _this.brickManager.ready && _this.dango.ready && _this.scoreBoard.ready;
	}
	
	var init = function () {
		// 初始化资源
		_this.bg = new Bg(new Vector2(0,0), new Rectangle(480, 768));
		_this.bg.img.src = "./images/loopBG.png";
		// 初始化砖块管理器
		_this.brickManager = new BrickManager(_this.logic);
		// 初始化分数板
		_this.scoreBoard = new ScoreBoard(new Vector2(10, 10), new Rectangle(50, 200), 0.5);
		
		//初始化时确定dango的坐标(位于第一块砖块上)
		_this.dango = new Dango(new Vector2(230, 500), new Rectangle(56, 30));
        _this.dango.physicsNode.velocity = new Vector2(0, -25);
        _this.dango.physicsNode.acceleration = new Vector2(0, 1);
		_this.keydown = false;
		_this.firstkeydown = true;
		//death flag(flase为未死亡, true为死亡)
		_this.deathFlag = false;
		
		_this.logic.marks = 0;
    }
	init();
}